package com.de.juxfux.db;

import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.ContextMenu.ContextMenuInfo;

import com.de.juxfux.Const;


public class Juxdata implements Parcelable, ContextMenuInfo {
	private int id;
	private String type;
	private String category;
	private String tags;
	private String text;

	public static final Parcelable.Creator<Juxdata> CREATOR = new Parcelable.Creator<Juxdata>() {
		public Juxdata createFromParcel(Parcel in) {
			return new Juxdata(in);
		}
		public Juxdata[] newArray(int size) {
			return new Juxdata[size];
		}
	};

	public Juxdata() {
		super();
	}
	
	/**
	 * Konstruktor, um die Daten aus einem Parcel zu rekonstruieren.
	 * Liest die sie in der gleichen Reihenfolge aus, in der sie in
	 * das Parcel hineingeschrieben wurden.
	 * 
	 * @param in - ein Parcel mit Daten
	 */
	public Juxdata(Parcel in) {
		super();
		this.id = in.readInt();
		this.type = in.readString();
		this.category = in.readString();
		this.tags = in.readString();
		this.text = in.readString();
	}
	
	public Juxdata(int id, String type, String category, String tags,
			String text) {
		super();
		this.id = id;
		this.type = type;
		this.category = category;
		this.tags = tags;
		this.text = text;
	}
	
    @Override
    public boolean equals (Object in) {
        if (in instanceof Juxdata) {
        	Juxdata inData = (Juxdata) in;
        	return (this.id == inData.getId() &&
        		this.type.equals(inData.getType()) &&
        		this.category.equals(inData.getCategory()) &&
        		this.tags.equals(inData.getTags()) &&
        		this.text.equals(inData.getText()));
        } else {
        	return false;
        }       
    }

	public static Juxdata fetchFrom(Cursor c) {
		Juxdata juxdata = new Juxdata();
		juxdata.setId(c.getInt(c.getColumnIndex(DbTableJuxdata.COLUMN_ID)));
		juxdata.setType(c.getString(c.getColumnIndex(DbTableJuxdata.COLUMN_TYPE)));
		juxdata.setCategory(c.getString(c.getColumnIndex(DbTableJuxdata.COLUMN_CAT)));
		juxdata.setTags(c.getString(c.getColumnIndex(DbTableJuxdata.COLUMN_TAGS)));
		juxdata.setText(c.getString(c.getColumnIndex(DbTableJuxdata.COLUMN_TEXT)));
		return juxdata;
	}
    
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	public String[] getTagsArray() {
		String[] tagWordsArray = getTags().split(Const.TAGSEP_REGEX);
		return tagWordsArray;
	}
	
	public String[] getTextWordsArray() {
		String[] textWordsArray = getText().split(Const.WORDSEP_REGEX);
		return textWordsArray;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeString(type);
		dest.writeString(category);
		dest.writeString(tags);
		dest.writeString(text);
	}
	
}